package com.zxjbyte.yiyi.extension.job.core.trigger.route;

import org.quartz.CalendarIntervalScheduleBuilder;

import java.util.Calendar;

import static org.quartz.CalendarIntervalScheduleBuilder.*;

/**
 * 日历时间间隔触发器
 * CalendarIntervalTrigger允许你基于日历的时间间隔触发任务。与DailyTimeIntervalTrigger不同，CalendarIntervalTrigger是基于日历的，
 * 它可以在指定的日历时间间隔内触发任务，而不仅仅是每天。
 *
 * @Author zhangxingjia
 * @Date 2023/10/8 15:42
 * @Version: 1.0
 */
public class CalendarIntervalTriggerHelper {

    /**
     * 创建每天触发一次的触发器
     * @return
     */
    public static CalendarIntervalScheduleBuilder dailyCaleSchedule(){
       return calendarIntervalSchedule().withIntervalInDays(1);
    }

    /**
     * 间隔秒数执行
     *
     * @param intervalInSeconds 触发器应重复的秒数
     * @return
     */
    public static CalendarIntervalScheduleBuilder intervalSecondsCaleSchedule(int intervalInSeconds){
        return calendarIntervalSchedule().withIntervalInSeconds(intervalInSeconds);
    }

    /**
     * 创建每小时触发一次的触发器
     * @return
     */
    public static CalendarIntervalScheduleBuilder hourlyCaleSchedule(){
        return calendarIntervalSchedule().withIntervalInHours(1);
    }

    /**
     * 创建每半小时触发一次的触发器
     * @return
     */
    public CalendarIntervalScheduleBuilder every30MinutesCaleSchedule() {
        return calendarIntervalSchedule().withIntervalInMinutes(30);
    }

    /**
     * 创建每月触发一次的触发器
     * @return
     */
    public CalendarIntervalScheduleBuilder monthlyCaleSchedule() {
        return calendarIntervalSchedule().withIntervalInMonths(1);
    }

    /**
     * 创建每年触发一次的触发器
     */
    public static CalendarIntervalScheduleBuilder yearlyCaleSchedule() {
        return calendarIntervalSchedule().withIntervalInYears(1);
    }

    /**
     * 自定义时间间隔触发
     * @param interval
     * @param intervalType
     * @return
     */
    public CalendarIntervalScheduleBuilder customCaleIntervalSchedule(int interval, int intervalType) {
        switch (intervalType) {
            case Calendar.SECOND:
                return calendarIntervalSchedule()
                        .withIntervalInSeconds(interval);
            case Calendar.MINUTE:
                return calendarIntervalSchedule()
                        .withIntervalInMinutes(interval);
            case Calendar.HOUR:
                return calendarIntervalSchedule()
                        .withIntervalInHours(interval);
            case Calendar.DAY_OF_MONTH:
                return calendarIntervalSchedule()
                        .withIntervalInDays(interval);
            default:
                throw new IllegalArgumentException("Invalid interval type");
        }
    }
}
